﻿using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.PortfolioManagement;
using Common.Contract.Customer.Response.PortfolioManagement;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Customer.Core.ServiceCommand.PortfolioManagement
{
    public class RetrieveAccountPlanningFinanceHistoryServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveAccountPlanningFinanceHistoryRequest, RetrieveAccountPlanningFinanceHistoryResponse>
    {
        #region Properties
        public string Cif;
        public string TypeHist;
        public string TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveAccountPlanningFinanceHistoryServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveAccountPlanningFinanceHistoryRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Cif))
            {
                throw new ArgumentException("Cif customer must be set", "request");
            }
            else
            {
                Cif = Request.Cif;
            }
            if (string.IsNullOrEmpty(request.Type))
            {
                throw new ArgumentException("Type must be set", "request");
            }
            else
            {
                TypeHist = Request.Type;
            }
        }

        protected override bool OnExecute(RetrieveAccountPlanningFinanceHistoryResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<AccountPlanningHistoryRecord> records = new List<AccountPlanningHistoryRecord>();
            List<AccountPlanningHistory> listCustomer = new List<AccountPlanningHistory>();
            listCustomer = Providers.Portfolios.RetrieveAccountPlanningHistoryByTypeCif(Cif, TypeHist);
            if (listCustomer != null)
            {
                records = listCustomer.Select(x => new AccountPlanningHistoryRecord
                {
                    Id = x.Id.ToString(),
                    ChangeDate = x.ChangeDate.ToString(),
                    Value = x.Value,
                    DaoChange = x.DaoChange,
                    Type = x.Type,
                    Cif = x.Cif,
                    IsCurrent = x.Id.ToString() == Request.Id ? true : false
                }).ToList();
            }
            response.Records = records;
            return isSuccess;
        }
    }
}
